package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.EntitySequenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EntityDeviceMapper;
import com.ruoyi.system.domain.EntityDevice;
import com.ruoyi.system.service.IEntityDeviceService;
import com.ruoyi.common.core.text.Convert;

import static com.ruoyi.common.constant.RentConstants.DEVICE_TYPE_IN_STOCK;
import static com.ruoyi.common.constant.RentConstants.DEVICE_TYPE_RENTING;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Service
public class EntityDeviceServiceImpl implements IEntityDeviceService 
{
    @Autowired
    private EntityDeviceMapper entityDeviceMapper;
    @Autowired
    private EntitySequenceMapper entitySequenceMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public EntityDevice selectEntityDeviceById(Long id)
    {
        return entityDeviceMapper.selectEntityDeviceById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param entityDevice 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<EntityDevice> selectEntityDeviceList(EntityDevice entityDevice)
    {
        return entityDeviceMapper.selectEntityDeviceList(entityDevice);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param entityDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public AjaxResult insertEntityDevice(EntityDevice entityDevice)
    {
        entityDevice.setCreateTime(DateUtils.getNowDate());
        String serialNumber = DateUtils.dateTimeNow("YYYYMMDD");
        if(entityDevice.getTypeId()<10){
            serialNumber = serialNumber + "0" + entityDevice.getTypeId();
        }else{
            serialNumber = serialNumber + entityDevice.getTypeId();
        }
        Long deviceNumber = entitySequenceMapper.getNextValueAtMax99("device_number");
        if(deviceNumber < 10){
            serialNumber = serialNumber + "0" + deviceNumber;
        }else{
            serialNumber = serialNumber + deviceNumber;
        }
        entityDevice.setSerialNumber(serialNumber);
        int i = entityDeviceMapper.insertEntityDevice(entityDevice);
        return i>0?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param entityDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public AjaxResult updateEntityDevice(EntityDevice entityDevice) {
        if(DEVICE_TYPE_RENTING == selectEntityDeviceById(entityDevice.getId()).getValid()){
            return AjaxResult.error("当前设备处于出租状态，不可修改！");
        }
        return 1==entityDeviceMapper.updateEntityDevice(entityDevice)?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEntityDeviceByIds(String ids)
    {
        return entityDeviceMapper.deleteEntityDeviceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteEntityDeviceById(Long id)
    {
        return entityDeviceMapper.deleteEntityDeviceById(id);
    }


    /**
     * 根据器材编号获取器材
     * @param serialNumber
     * @return
     */
    public EntityDevice selectEntityDeviceBySerialNumber(String serialNumber){
        return entityDeviceMapper.selectEntityDeviceBySerialNumber(serialNumber);
    }

    /**
     * 归还设备
     * @param deviceId
     * @return
     */
    @Override
    public AjaxResult returnDevice(Long deviceId){
        EntityDevice device = new EntityDevice();
        device.setId(deviceId);
        device.setValid(DEVICE_TYPE_IN_STOCK);
        return updateEntityDevice(device);
    }
}
